// pages/emergency/workBriefAdd/workBriefAdd.js
var app = getApp()
var util = require('../../../utils/util.js')

Page({

    /**
     * 页面的初始数据
     */
    data: {
        workBrief: {},
        add: false,//是否为添加简报,
        update: false,//对简报进行更新
        reportTimeStr: '',
        defenseWork: '',//现阶段防御工作部署落实情况
        dutyPeople: '',//值班人员到位情况
        disasterSituation: '',//受损受灾情况
        emergency: '',//突发事件情况
        emergencyHandle: '',//事件现场应急处置及抢险救灾情况
        emergencyAfter: '',//事件应急结束善后处置情况
        emgPlanPublicId: 0,
        wbId: 0,//工作简报的id
        requestUrl: "",//请求url
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var add = options.add

        if (add == "false") {//更新或者发布已经保存的工作简报
            var workBriefJson = JSON.parse(options.workBrief)
           this.loadWorkBriefDetail(workBriefJson.wbId)


        } else {//发布或者保存，没有发不过的简报
            this.setData({
                add: true,
                update: false,
                emgPlanPublicId: options.emgPlanPublicId,
                requestUrl: '/safe/workBrief/add'

            })
        }
    },

    loadWorkBriefDetail(wbId){
        var that = this
        var header;
        header = {
            'Cookie': wx.getStorageSync("sessionid"), //读取cookie
            'content-type': 'application/x-www-form-urlencoded'
        };
        var params = {
           wbId:wbId,
        }
        wx.request({
            header: header,
            url: app.globalData.baseUrl + '/safe/workBrief/find',
            method: "POST",
            data: params,
            success(res) {
                if (res.data.success&& res.data.data.page.size>0) {
                    var workBrief=res.data.data.page.list[0];
                    that.setData({
                        defenseWork: workBrief.defenseWork,
                        dutyPeople: workBrief.dutyPeople,
                        disasterSituation: workBrief.disasterSituation,
                        emergency: workBrief.emergency,
                        emergencyHandle: workBrief.emergencyHandle,
                        emergencyAfter: workBrief.emergencyAfter,
                        reportTimeStr: util.formatTimeHour(new Date(workBrief.reportTime)),
                        workBrief: workBrief,
                        status: workBrief.status,
                        wbId: workBrief.wbId,
                        emgPlanPublicId: workBrief.planPublicId
                    })
                    if (workBrief.status == 0) {
                        that.setData({
                            add: false,
                            update: true,
                            requestUrl: "/safe/workBrief/update"
                        })
                    } else if (workBrief.status == 1) {
                        that.setData({
                            add: false,
                            update: false,
                        })
                    }
                }else{
                    wx.showToast({
                      title: '没有该工作简报'

                    })
                }
            }
        })
    },
    back() {
        wx.navigateBack({
            delta: 1
        })
    },
    /**
     * 保存
     */
    saveWorkBrief() {
        var that = this
        var header;
        header = {
            'Cookie': wx.getStorageSync("sessionid"), //读取cookie
            'content-type': 'application/x-www-form-urlencoded'
        };
        var params = {
            planPublicId: this.data.emgPlanPublicId,
            companyId: app.globalData.user.companyId,
            deptId: app.globalData.user.deptId,
            defenseWork: this.data.defenseWork,
            dutyPeople: this.data.dutyPeople,
            disasterSituation: this.data.disasterSituation,
            emergency: this.data.emergency,
            emergencyHandle: this.data.emergencyHandle,
            emergencyAfter: this.data.emergencyAfter,
            reportUserId: app.globalData.user.userId,
            status: 0,
            reportTime:new Date()

        }

        if (this.data.add) {
            wx.request({
                header: header,
                url: app.globalData.baseUrl + this.data.requestUrl,
                method: "POST",
                data: params,
                success(res) {
                    if (res.data.success) {
                        var wokrBrief = res.data.data
                        //TODO
                        wx.navigateBack({
                            delta: 1
                        })
                    }
                }
            })
        }
        if (this.data.update) {
            params.wbId = this.data.wbId
            wx.request({
                header: header,
                url: app.globalData.baseUrl + this.data.requestUrl,
                method: "POST",
                data: params,
                success(res) {
                    if (res.data.success) {
                        var wokrBrief = res.data.data
                        //TODO
                        wx.navigateBack({
                            delta: 1
                        })
                    }
                }
            })
        }

    },
    /**
     * 发布
     */
    publicWorkBrief() {
        var that = this
        var header;
        header = {
            'Cookie': wx.getStorageSync("sessionid"), //读取cookie
            'content-type': 'application/x-www-form-urlencoded'
        };
        var params = {
            planPublicId: this.data.emgPlanPublicId,
            companyId: app.globalData.user.companyId,
            deptId: app.globalData.user.deptId,
            defenseWork: this.data.defenseWork,
            dutyPeople: this.data.dutyPeople,
            disasterSituation: this.data.disasterSituation,
            emergency: this.data.emergency,
            emergencyHandle: this.data.emergencyHandle,
            emergencyAfter: this.data.emergencyAfter,
            reportUserId: app.globalData.user.userId,
            status: 1,
        }
        if (this.data.add) {
            wx.request({
                header: header,
                url: app.globalData.baseUrl + this.data.requestUrl,
                method: "POST",
                data: params,
                success(res) {
                    if (res.data.success) {
                        var wokrBrief = res.data.data
                        //TODO
                        wx.navigateBack({
                            delta: 1
                        })
                    }
                }
            })
        }
        if (this.data.update) {
            params.wbId = this.data.wbId
            wx.request({
                header: header,
                url: app.globalData.baseUrl + this.data.requestUrl,
                method: "POST",
                data: params,
                success(res) {
                    if (res.data.success) {
                        var wokrBrief = res.data.data

                        wx.navigateBack({
                            delta: 1
                        })
                    }
                }
            })
        }

    },
    defenseWorkChange(e) {
        this.setData({
            defenseWork: e.detail.value
        })
    },
    dutyPeopleChange(e) {
        this.setData({
            dutyPeople: e.detail.value
        })
    },
    disasterSituationChange(e) {
        this.setData({
            disasterSituation: e.detail.value
        })
    },
    emergencyChange(e) {
        this.setData({
            emergency: e.detail.value
        })
    },
    emergencyHandleChange(e) {
        this.setData({
            emergencyHandle: e.detail.value
        })
    },
    emergencyAfterChange(e) {
        this.setData({
            emergencyAfter: e.detail.value
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})